// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import gameData from "../gameData";
import sceneMediator from "../../framework/sceneMediator";
import AudioManage, {soundList} from "../../framework/AudioManage";
import Tool from "../../framework/Tool";

const {ccclass, property} = cc._decorator;

@ccclass
export default class endlessWin extends cc.Component {

    @property(cc.Node)
    labList : cc.Node[] = [];

    showRecorderShare = false;

    clickBackHome = false;

    isEndless = false;

    onLoad(){
        AudioManage.playSFX(soundList.colour);


        if(this.clickBackHome){
            if(gameData.finalAppleGet){
                this.labList[2].active = true;
                this.labList[0].active = false;
            }
        }else{
            if(this.isEndless &&　gameData.showRewardApple){
                this.labList[1].active = true;
                this.labList[0].active = false;
            }
        }
    }

    setCallBack(showShare,isEndless,backHome?){
        this.showRecorderShare = showShare;
        this.isEndless = isEndless;
        if(backHome){
            this.clickBackHome = backHome;
        }
    }

    btnClick(event,data){
        if(parseInt(data) == 1){
            if(!this.clickBackHome){
                if(this.showRecorderShare){
                    if(!gameData.showRewardApple){
                        gameData.addSinglePumpkin(10);
                        gameData.saveShowRewardApple();
                    }
                    Tool.loadPrefab("prefabs/RecorderShare",(res)=>{
                        let view = cc.instantiate(res);
                        view.getComponent("recorderShare").setViewType(true,()=>{
                            cc.director.loadScene("main",()=>{
                                Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                                    let view = cc.instantiate(res);
                                    view.parent = cc.find("Canvas");
                                });
                            })
                        });
                        view.parent = cc.find("Canvas");
                    });
                }else{
                    gameData.addSinglePumpkin(10);
                    sceneMediator.LoadCurScene();
                }
            }else{
                if(!gameData.finalAppleGet){
                    gameData.finalAppleGet = true;
                    gameData.saveOtherGameData();
                    gameData.addSinglePumpkin(10);
                    sceneMediator.backToHome();
                }else{
                    sceneMediator.backToHome();
                }
            }
        }

        if(parseInt(data) == 2){
            sceneMediator.backToHome();
        }
    }
}
